#include <stdint.h>
#include "MKE06Z4.h"
#include "system_MKE06Z4.h"
#include "uart.h"
#include "systimer.h"
#include "mytype.h"
#include "mbslave.h"
#include "mbmaster.h"
#include "temperature.h"
#include "keycheck.h"

void DataInit(void);
void PinConfig(void);
void LEDConfig(void);

uint32_t TestCnt = 0;

int main()
{
	SystemCoreClockUpdate();
	SysTickInit();
	PinConfig();
	
//	KBIInit();
//	mbInit(19200, MB_PAR_EVEN );
	
	DataInit();
	
	while(1)
	{
		SysPulseCreat();
		/*-----------------------------------------------------*/
		if(bPulse_3s)
		{
		
		}

		TempADSet(1 ,12000);
		TempCal(1);
		TestCnt++;

		LEDConfig();
//		KeyHandle();
		
		mbSlavePoll();
		mbMasterPoll();
		
		/*-----------------------------------------------------*/
		SysPulseClear();
	}
}


void DataInit()
{
	TempMarkTable[1].MarkADL = 2000;
	TempMarkTable[1].MarkADH = 18000;
	TempCorrectTable[1].PB   = 0;
	TempCorrectTable[1].PK   = 0;
	TempLimiteTable[1].LimiteH = 300;
	TempLimiteTable[1].LimiteL = -100;
	
}

void PinConfig()
{
	//Port Filter Config
	PORT->IOFLT0 =  (4UL << PORT_IOFLT0_FLTDIV3_SHIFT) |           //16ms
					(1UL << PORT_IOFLT0_FLTDIV2_SHIFT) |           //BusClk / 64
					(3UL << PORT_IOFLT0_FLTDIV1_SHIFT) |           //BusClk / 16
					(3UL << PORT_IOFLT0_FLTNMI_SHIFT)  |           //DIV3 for NMI
					(3UL << PORT_IOFLT0_FLTKBI0_SHIFT) |           //DIV3 for KBI0
					(3UL << PORT_IOFLT0_FLTKBI1_SHIFT) |           //DIV3 for KBI1
					(3UL << PORT_IOFLT0_FLTRST_SHIFT)  |           //DIV3 for RST
					(0UL << PORT_IOFLT0_FLTH_SHIFT)    |           // for PTH
					(0UL << PORT_IOFLT0_FLTG_SHIFT)    |           // for PTG
					(0UL << PORT_IOFLT0_FLTF_SHIFT)    |           // for PTF
					(0UL << PORT_IOFLT0_FLTE_SHIFT)    |           // for PTE
					(0UL << PORT_IOFLT0_FLTD_SHIFT)    |           // for PTD
					(0UL << PORT_IOFLT0_FLTC_SHIFT)    |           // for PTC
					(0UL << PORT_IOFLT0_FLTB_SHIFT)    |           // for PTB
					(0UL << PORT_IOFLT0_FLTA_SHIFT);               // for PTA
					                  
	PORT->IOFLT1 =  (0UL << PORT_IOFLT1_FLTI2C1_SHIFT) |           // for I2C1
					(0UL << PORT_IOFLT1_FLTI2C0_SHIFT) |           // for I2C0
					(0UL << PORT_IOFLT1_FLTPWT_SHIFT)  |           // for PWT
					(0UL << PORT_IOFLT1_FLTFTM1_SHIFT) |           // for FTM1
					(0UL << PORT_IOFLT1_FLTFTM0_SHIFT) |           // for FTM0
					(0UL << PORT_IOFLT1_FLTIRQ_SHIFT)  |           // for IRQ
					(0UL << PORT_IOFLT1_FLTI_SHIFT);               // for PTI
	
	
	PORT->PUE0   = 0x00000000;         //Pullup Enable : A B C D
	PORT->PUE1   = 0x00FF0010;         //Pullup Enable : E F G H
	PORT->PUE2   = 0x00000000;         //Pullup Enable : I
	
	PORT->HDRVE  = 0;                  //High Drive Enable
	
	FGPIOA->PDDR = 0x03000000;         // Port Data Direction : A B C D 
	FGPIOB->PDDR = 0x00FF0010;         // Port Data Direction : E F G H
	FGPIOC->PDDR = 0x00000000;         // Port Data Direction : I
	
	FGPIOB->PIDR = 0xFCFFFFFF;         // Port Input Disable  : A B C D
	FGPIOB->PIDR = 0x1BFFFF7F;         // Port Input Disable  : E F G H
	FGPIOB->PIDR = 0xFFFFFFFF;         // Port Input Disable  : I
	
	FGPIOB->PTOR = 0x00FF0010;         // Port Data Out: E F G H 
}

void LEDConfig()
{
	static uint8_t LedData  = 0xFF;
	static uint8_t LedLight = 0xFF;
	static uint8_t LedRun   = 0xFF;
	
	if(bPulse_1s)
	{
		switch(LedRun)
		{
			case 0x18:
				LedRun = 0x24;break;
			case 0x24:
				LedRun = 0x42;break;
			case 0x42:
				LedRun = 0x81;break;
			case 0x81:
				LedRun = 0x18;break;
			default:
				LedRun = 0x81;break;
		}
	}	
	
	if(bPulse_250ms)
	{
		LedLight >>= 1;

		if(0 == LedLight)
		{
			LedLight = 0x80;
		}
	}
	
	if(bPulse_100ms)
	{
		LedData <<= 1;
		if(0 == LedData)
		{
			LedData = 0x01;
		}
	}		
	
	
	if(bPulse_50ms)
	{	
		FGPIOB->PCOR = 0x00FF0010;
		FGPIOB->PSOR = ((uint32_t)((~(LedData | LedLight | LedRun)))) << 16;
		__nop();__nop();__nop();__nop();__nop();
		FGPIOB->PSOR = 0x00000010;	
	}
}
